package com.fc.push.handler.pub.exec;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class PublishTaskPool {
	private final static int KEEP_ALIVE_TIME = 60;

	private static int MAXSIZE = 1;
	private static int MINSIZE = 1;

	private static final int INIT_QUEUE_SIZE = 500;
	
	private static PublishTaskPool publishTaskPool = null;
	private static ThreadPoolExecutor poolExecutor = null;
	private static AtomicLong tId = new AtomicLong(1);
	private PublishTaskPool() {
		MINSIZE = Runtime.getRuntime().availableProcessors();
		MAXSIZE = MINSIZE + 1;
		poolExecutor = new ThreadPoolExecutor(MINSIZE, MAXSIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS,
				new LinkedBlockingQueue<Runnable>(INIT_QUEUE_SIZE),new ThreadFactory() {
			
			@Override
			public Thread newThread(Runnable r) {
				Thread thread = new Thread(r,"Pthread_" + tId.getAndIncrement());
				thread.setDaemon(false);
				return thread;
			}
		});
	}

	public static class Instance {
		public static PublishTaskPool getInstance() {
			if(publishTaskPool == null){
				publishTaskPool = new PublishTaskPool();
			}
			return publishTaskPool;
		}
	}

	public void submit(Runnable task) {
		poolExecutor.submit(task);
	}
	
	public void execute(Runnable task){
		poolExecutor.execute(task);
	}
	
	public static void main(String[] args){
		PublishTaskPool.Instance.getInstance().submit(new Runnable() {
			
			@Override
			public void run() {
				System.out.println("abc");
			}
		});
	}
	
}
